/*
 *  Copyright (C) 2010 glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction.view;

import java.util.Stack;
import javax.swing.JFrame;
import org.balisunrise.application.Application;
import org.balisunrise.application.View;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.EntitySelection;
import org.balisunrise.common.Search;
import org.balisunrise.common.VisualEntity;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.swing.transaction.Transaction;

/**
 *
 * @author Glauco
 */
public class ViewManager implements org.balisunrise.application.ViewManager{

    private Application app;

    private Stack<View> showedViews;

    public Application getApp() {
        if (app == null)
            app = Application.get();
        return app;
    }

    public void setApp(Application app) {
        this.app = app;
    }

    public Stack<View> getShowedViews() {
        if(showedViews == null)
            showedViews = new Stack<View>();
        return showedViews;
    }

    public void setShowedViews(Stack<View> showedViews) {
        this.showedViews = showedViews;
    }

    public View showVisualEntity(VisualEntityType type, Class entityClass,
            String alias) throws BuilderException {

        VisualEntity ve = getApp().getFactory().createVisualEntity(type,
                entityClass, alias);

        return showVisualEntity(ve);
    }

    public View showVisualEntity(VisualEntity visualEntity) {

        View v = createView(visualEntity);

        showView(v);

        return v;
    }

    public void showView(View view) {
        getShowedViews().push(view);
        view.setVisible(true);
        getShowedViews().pop();
    }

    public View getActiveView() {
        
        if (getShowedViews().empty())
            return null;

        return getShowedViews().peek();
    }

    public View createView(VisualEntity ve){

        View v;
        switch (ve.getVisualEntityType()) {
            case TRANSACTION:
                v = new org.balisunrise.swing.transaction.view.View(
                        getApp(),
                        new TransactionDialog(
                            (JFrame) getApp().getVisualRepresentation(),
                            true,
                            (Transaction) ve));
                break;
            case SEARCH:
                v = new org.balisunrise.swing.transaction.view.View(
                        getApp(),
                        new SearchDialog(
                            (JFrame) getApp().getVisualRepresentation(),
                            (Search) ve));
                break;
            case ENTITY_SELECTION:
                v = new org.balisunrise.swing.transaction.view.View(
                        getApp(),
                        new SelectionDialog(
                            (JFrame)getApp().getVisualRepresentation(),
                            (EntitySelection) ve));
                break;
            case ENTITY_PANEL:
                v = new org.balisunrise.swing.transaction.view.View(
                        getApp(),
                        new SimpleDialog(
                            (JFrame)getApp().getVisualRepresentation(), ve));
                break;
            default:
                throw new AssertionError();
        }

        ve.setOwner(v);
        v.setOwner(Application.get());
        return v;
    }
}
